/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package pl.droidsonroids.gif.gifsample;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import ohos.aafwk.ability.DataAbilityRemoteException;
import ohos.aafwk.ability.delegation.AbilityDelegatorRegistry;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.Image;
import ohos.app.Context;
import pl.droidsonroids.gif.GifOptions;
import pl.droidsonroids.gif.GifTexImage2D;
import pl.droidsonroids.gif.InputSource;

import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

/**
 * Gif2dTest
 */
public class Gif2dTest {
    private static final int FRAME_COUNT = 4;  // Number of frames in the given gif file

    private static final int FRAME_INDEX_2 = 2;

    private GifTexImage2D image2D;
    private Context mContext;
    private ComponentContainer rootLayout;
    private Image image;

    /**
     * setUp
     */
    @Before
    public void setUp() {
        mContext = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();

        InputSource.AssetSource source = new InputSource.AssetSource(mContext.getResourceManager()
            .getRawFileEntry("entry/resources/rawfile/anim_flag_england.gif"), "");
        GifOptions options = new GifOptions();
        options.setInIsOpaque(true);
        try {
            image2D = new GifTexImage2D(source, options);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (DataAbilityRemoteException e) {
            e.printStackTrace();
        }
        image2D.startDecoderThread();
    }

    /**
     * testDecoderInstance
     */
    @Test
    public void testDecoderInstance() {
        assertNotNull(image2D);
    }

    /**
     * testWidth
     */
    @Test
    public void testWidth() {
        assertTrue(image2D.getWidth() > 0);
    }

    /**
     * testHeight
     */
    @Test
    public void testHeight() {
        assertTrue(image2D.getHeight() > 0);
    }

    /**
     * testFrameDuration
     */
    @Test
    public void testFrameDuration() {
        assertTrue(image2D.getFrameDuration(1) > 0);
    }

    /**
     * testNumberOfFrames
     */
    @Test
    public void testNumberOfFrames() {
        assertEquals(image2D.getNumberOfFrames(), FRAME_COUNT);
    }

    /**
     * testSeekToFrame
     */
    @Test
    public void testSeekToFrame() {
        image2D.seekToFrame(FRAME_INDEX_2 - 1); // Frames starts from 0
        assertEquals(image2D.getCurrentFrameIndex(), FRAME_INDEX_2);
    }


}